/**
 * Class:   ActivityRelationsManager
 * 
 * Methods:
 * 
 *      boolean insertActivityOrganizationRelation( int activityID,
 *      											int organizationID );
 *      
 *      boolean insertActivityBeneficiaryRelation( int activityID,
 *      										   int beneficiaryID );
 *      
 *      boolean insertActivityAdviserRelation( int activityID, int adviserID );
 *      
 *      boolean insertActivityParticipantRelation( int activityID,
 *      										   int participantID );
 *      
 *      boolean deleteActivityOrganizationRelation( int activityID,
 *      											int organizationID );
 *      
 *      boolean deleteActivityBeneficiaryRelation( int activityID,
 *      										   int beneficiaryID );
 *      
 *      boolean deleteActivityAdviserRelation( int activityID, int adviserID );
 *      
 *      boolean deleteActivityParticipantRelation( int activityID,
 *      										   int participantID );
 * 
 * Remarks:
 * 
 * 		This class should not be touched by the controllers.
 * 
 * @author ( Sharmaine Lim )
 * @version ( 2013-02-28 // YYYY-MM-DD )
 */

package managers;

import java.sql.PreparedStatement;

class VolunteerActivityRelationsManager extends DatabaseManager {
	
    /**
     * Constructors
     */
    
    VolunteerActivityRelationsManager() {}
    
    
    
    /**
     *  Insert Activity_Organization Relation
     *  	- inserts the link of an activity and an organization
     *  	  into the v_activityOrganization table
     *  
     *  Parameters
     *		- int activityID: ID of activity to be added to the table
     *		- int organizationID: ID of organization to be added to
     *				the table
     *	
     *	Returns
     *		- boolean true: activity relation is successfully inserted
     * 		- boolean false: insertion failed
     */
    boolean insertActivityOrganizationRelation( int activityID,
    											int organizationID ) {
    	try {
    		/* Declaration of Block Variables */
        	PreparedStatement preparedStatement;
        	
			connect();
    		
    		preparedStatement = getConnection()
    							.prepareStatement( "INSERT INTO" +
    											   " `v_activityOrganization`" +
    											   " (`activityID`," +
    											   " `organizationID`)" +
    											   " VALUES (?, ?)" );
    		
            preparedStatement.setInt( 1, activityID );
            preparedStatement.setInt( 2, organizationID );
            preparedStatement.executeUpdate();
            
            disconnect();
            return true;
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Insert Activity_Beneficiary Relation
     *  	- inserts the link of an activity and a beneficiary
     *  	  into the v_activityBeneficiary table
     *  
     *  Parameters
     *		- int activityID: ID of activity to be added to the table
     *		- int beneficiaryID: ID of beneficiary to be added to the
     *				table
     *	
     *	Returns
     *		- boolean true: activity relation is successfully inserted
     * 		- boolean false: insertion failed
     */
    boolean insertActivityBeneficiaryRelation( int activityID,
    										   int beneficiaryID ) {
    	try {
    		/* Declaration of Block Variables */
        	PreparedStatement preparedStatement;
        	
			connect();
        	
    		preparedStatement = getConnection()
    							.prepareStatement( "INSERT INTO" +
    											   " `v_activityBeneficiary`" +
    											   " (`activityID`," +
    											   " `beneficiaryID`)" +
    											   " VALUES (?, ?)" );
    		
            preparedStatement.setInt( 1, activityID );
            preparedStatement.setInt( 2, beneficiaryID );
            preparedStatement.executeUpdate();
            
            disconnect();
            return true;
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Insert Activity_Adviser Relation
     *  	- inserts the link of an activity and an adviser
     *  	  into the v_activityAdviser table
     *  
     *  Parameters
     *		- int activityID: ID of activity to be added to the table
     *		- int adviserID: ID of adviser to be added to the table
     *	
     *	Returns
     *		- boolean true: activity relation is successfully inserted
     * 		- boolean false: insertion failed
     */
    boolean insertActivityAdviserRelation( int activityID, int adviserID ) {
    	try {
    		/* Declaration of Block Variables */
        	PreparedStatement preparedStatement;
        	
			connect();
        	
    		preparedStatement = getConnection()
    							.prepareStatement( "INSERT INTO" +
    											   " `v_activityAdviser`" +
    											   " (`activityID`," +
    											   " `adviserID`)" +
    											   " VALUES (?, ?)" );
    		
            preparedStatement.setInt( 1, activityID );
            preparedStatement.setInt( 2, adviserID );
            preparedStatement.executeUpdate();
            
            disconnect();
            return true;
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Insert Activity_Participant Relation
     *  	- inserts the link of an activity and an participant
     *  	  into the v_activityParticipant table
     *  
     *  Parameters
     *		- int activityID: ID of activity to be added to the table
     *		- int participantID: ID of participant to be added to the table
     *	
     *	Returns
     *		- boolean true: activity relation is successfully inserted
     * 		- boolean false: insertion failed
     */
    boolean insertActivityParticipantRelation( int activityID,
    										   int participantID ) {
    	try {
    		/* Declaration of Block Variables */
        	PreparedStatement preparedStatement;
        	
			connect();
        	
    		preparedStatement = getConnection()
    							.prepareStatement( "INSERT INTO" +
    											   " `v_activityParticipant`" +
    											   " (`activityID`," +
    											   " `participantID`)" +
    											   " VALUES (?, ?)" );
    		
            preparedStatement.setInt( 1, activityID );
            preparedStatement.setInt( 2, participantID );
            preparedStatement.executeUpdate();
            
            disconnect();
            return true;
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Delete Activity_Organization Relation
     *  	- deletes the link of an activity and an organization
     *  	  from the v_activityOrganization table
     *  
     *  Parameters
     *		- int activityID: ID of activity to be deleted from the
     *				table
     *		- int organizationID: ID of organization to be deleted
     *				from the table
     *	
     *	Returns
     *		- boolean true: activity relation is successfully deleted
     * 		- boolean false: deletion failed
     */
    boolean deleteActivityOrganizationRelation( int activityID,
    											int organizationID ) {
    	try {
    		/* Declaration of Block Variables */
    		String query = "DELETE FROM `v_activityOrganization` WHERE" +
    					   " `activityID` = ?";
        	PreparedStatement preparedStatement;
        	
			if ( organizationID != 0 ) {
    			query = query.concat( " AND `organizationID` = ?" );
    		}
			
			connect();
    		
			preparedStatement = getConnection().prepareStatement( query );
			
			preparedStatement.setInt( 1, activityID );
    		
			if ( organizationID != 0 ) {
    			preparedStatement.setInt( 2, organizationID );
    		}
			
			preparedStatement.executeUpdate();
    		
            disconnect();
            return true;
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Delete Activity_Beneficiary Relation
     *  	- deletes the link of an activity and a beneficiary
     *  	  from the v_activityBeneficiary table
     *  
     *  Parameters
     *		- int activityID: ID of activity to be deleted from the
     *				table
     *		- int beneficiaryID: ID of beneficiary to be deleted
     *				from the table
     *	
     *	Returns
     *		- boolean true: activity relation is successfully deleted
     * 		- boolean false: deletion failed
     */
    boolean deleteActivityBeneficiaryRelation( int activityID,
    										   int beneficiaryID ) {
    	try {
    		/* Declaration of Block Variables */
    		String query = "DELETE FROM `v_activityBeneficiary` WHERE" +
    					   " `activityID` = ?";
        	PreparedStatement preparedStatement;
        	
			if ( beneficiaryID != 0 ) {
    			query = query.concat( " AND `beneficiaryID` = ?" );
    		}
			
			connect();
    		
			preparedStatement = getConnection().prepareStatement( query );
			
			preparedStatement.setInt( 1, activityID );
    		
			if ( beneficiaryID != 0 ) {
    			preparedStatement.setInt( 2, beneficiaryID );
    		}
			
			preparedStatement.executeUpdate();
    		
            disconnect();
            
            new BeneficiaryManager().deleteBeneficiaryWithNoActivity();
            
            return true;
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Delete Activity_Adviser Relation
     *  	- deletes the link of an activity and an adviser from
     *  	  the v_activityAdviser table
     *  
     *  Parameters
     *		- int activityID: ID of activity to be deleted from the
     *				table
     *		- int adviserID: ID of adviser to be deleted from the
     *				table
     *	
     *	Returns
     *		- boolean true: activity relation is successfully deleted
     * 		- boolean false: deletion failed
     */
    boolean deleteActivityAdviserRelation( int activityID, int adviserID ) {
    	try {
    		/* Declaration of Block Variables */
    		String query = "DELETE FROM `v_activityAdviser` WHERE" +
    					   " `activityID` = ?";
        	PreparedStatement preparedStatement;
        	
			if ( adviserID != 0 ) {
    			query = query.concat( " AND `adviserID` = ?" );
    		}
			
			connect();
    		
			preparedStatement = getConnection().prepareStatement( query );
			
			preparedStatement.setInt( 1, activityID );
    		
			if ( adviserID != 0 ) {
    			preparedStatement.setInt( 2, adviserID );
    		}
			
			preparedStatement.executeUpdate();
    		
            disconnect();
            
            new AdviserManager().deleteAdviserWithNoActivity();
            
            return true;
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Delete Activity_Participant Relation
     *  	- deletes the link of an activity and a participant
     *  	  from the v_activityOrganization table
     *  
     *  Parameters
     *		- int activityID: ID of activity to be deleted from the
     *				table
     *		- int organizationID: ID of participant to be deleted
     *				from the table
     *	
     *	Returns
     *		- boolean true: activity relation is successfully deleted
     * 		- boolean false: deletion failed
     */
    boolean deleteActivityParticipantRelation( int activityID,
    										   int participantID ) {
    	try {
    		/* Declaration of Block Variables */
    		String query = "DELETE FROM `v_activityParticipant` WHERE" +
    					   " `activityID` = ?";
        	PreparedStatement preparedStatement;
        	
			if ( participantID != 0 ) {
    			query = query.concat( " AND `participantID` = ?" );
    		}
			
			connect();
    		
			preparedStatement = getConnection().prepareStatement( query );
			
			preparedStatement.setInt( 1, activityID );
    		
			if ( participantID != 0 ) {
    			preparedStatement.setInt( 2, participantID );
    		}
			
			preparedStatement.executeUpdate();
    		
            disconnect();
            
            new ParticipantManager().deleteParticipantWithNoActivity();
            
            return true;
    	}
    	catch ( Exception ex ) {
    		ex.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
}
